
require "LevelHelper.Helpers.LHDictionary"
require "LevelHelper.Nodes.LHSprite"
local lh_untitledBatchCount = 0;

LHBatch = Core.class(Sprite)
function LHBatch:batchWithDictionary(dictionary)

    if (nil == dictionary) then
        print("Invalid LHLayer initialization!")
    end
                
    object = self;
    
    --add all LevelHelper valid properties to the object
    object.lhNodeType = "LHBatch"
    object.lhImagePath = dictionary:stringForKey("SheetImage")
    object.lhZOrder = dictionary:intForKey("ZOrder")
    object.lhSHFile = nil -- just a reminder that this is here
    
    
    uName = dictionary:stringForKey("UniqueName");
    if(uName)then
       object.lhUniqueName = uName;
    else 
        uName = dictionary:stringForKey("SheetName");
        if(uName)then
            object.lhUniqueName = uName;
        else
           object.lhUniqueName = "UntitledBatch_" .. lh_untitledBatchCount;
           lh_untitledBatchCount = lh_untitledBatchCount + 1;
       end
    end
    
    self:setUserClassProperties(dictionary);
    
    local childsInfo = dictionary:objectForKey("Children");
    if(childsInfo~=nil)then
        local childrenInfo = childsInfo:arrayValue();
        if(childrenInfo~=nil)then
            for i=1,childrenInfo:count() do
                local childDict = childrenInfo:dictAtIndex(i);
                self:addBatchChildFromDictionary(childDict);
            end
        end
    end
    
    return self
end
--------------------------------------------------------------------------------
function LHBatch:userCustomInfoClassName()
    return self.lhClassName   
end
--------------------------------------------------------------------------------
function LHBatch:userCustomInfo()
     return self.lhUserCustomInfo;
end
--------------------------------------------------------------------------------
function LHBatch:addBatchChildFromDictionary(childDict)

    nodeType = childDict:stringForKey("NodeType");
    
    if(nodeType == "LHSprite")then
        local spr = LHSprite.new();
        spr:spriteWithDictionary(childDict);
        if(spr ~= nil)then
            self:addChild(spr, spr.lhZOrder);
            spr.lhParentLoader = self.lhParentLoader;        
        end
        return;
    end

    if(nodeType == "LHBezier")then
        print("ERROR: Batch nodes should not have LHBezier as children.");
        return;
    end
    
    if(nodeType == "LHBatch")then
        print("ERROR: Batch nodes should not have LHBatch as children.");
        return;
    end
    
    if(nodeType == "LHLayer")then
        print("ERROR: Batch nodes should not have LHLayer as children.");
        return;
    end
end
--------------------------------------------------------------------------------
function LHBatch:uniqueName()
    return self.lhUniqueName;
end
--------------------------------------------------------------------------------
function LHBatch:removeSelf() --this will also remove all children

    --print("calling LHBatch remove self " .. self.lhUniqueName .. " children " .. tostring(self.numChildren));
    
    --we use while because if we use for - when removing object we may lose some other objects that now have a lower index
    while(self:getNumChildren()  ~= 0) do
        local node = self:getChildAt(1)
        if(node)then
            node:removeSelf()
        end
        node = nil;
    end
    
    self:removeFromParent();
end
--------------------------------------------------------------------------------
function LHBatch:spriteWithUniqueName(name)--returns LHSprite with name (displayObject)
    
    for i = 1, self:getNumChildren() do

        local node = self:getChildAt(i);
        if(nil ~= node and node.lhNodeType ~= nil)then    
            if(node.lhNodeType == "LHSprite")then
            
                if(node.lhUniqueName ~= nil)then
                    if(node.lhUniqueName == name)then
                        return node;
                    end
                end
            end
        end
    end
    
    return nil;
end
--------------------------------------------------------------------------------
function LHBatch:allSprites()--returns array with LHSprite objects

    --we only have to put the sprites from self to a table
    local spritesTable = {}
    for i = 1, self:getNumChildren() do
        spritesTable[#spritesTable+1] = self:getChildAt(i);
    end

    return spritesTable    
end
--------------------------------------------------------------------------------
function LHBatch:spritesWithTag(tag) --returns array with LHSprite objects with tag

    local spritesTable = {}
    for i = 1, self:getNumChildren() do
        local spr = self:getChildAt(i);
        
        if(spr.lhTag and spr.lhTag == tag)then
            spritesTable[#spritesTable+1] = spr;
        end
    end

    return spritesTable
end
--------------------------------------------------------------------------------
function LHBatch:setUserClassProperties(spriteInfo)

    local classInfo = spriteInfo:dictForKey("CustomClassInfo");

    if(nil == classInfo)then
        return
    end
    
    self.lhClassName = classInfo:stringForKey("ClassName");
    
    if(self.lhClassName == nil)then
        return
    end
    
    require("LevelHelper.CustomClasses.LHCustomClasses");
    self.lhUserCustomInfo = lh_customClassInstanceWithName(self.lhClassName);

    if(self.lhUserCustomInfo == nil)then
        return
    end
    
    local classRep = classInfo:dictForKey("ClassRepresentation");
    
    self.lhUserCustomInfo:setPropertiesFromDictionary(classRep);
end
--------------------------------------------------------------------------------
